//
//  YDBankListViewController.m
//  PeiZiWangBei
//
//  Created by 巨彦新 on 2018/1/3.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "YDBankListViewController.h"
#import "YDBankListCell.h"
#import "YDBankCardModel.h"
#import "YDBindBankCardController.h"
#import "YDUnBindCardViewController.h"

@interface YDBankListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView *tableView;
//已绑卡数组
@property (nonatomic , strong) NSMutableArray *tiedCardArray;



@end

@implementation YDBankListViewController


-(NSMutableArray *)tiedCardArray
{
    if (_tiedCardArray == nil) {
        _tiedCardArray = [NSMutableArray array];
    }
    return _tiedCardArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNav];
   // [self tableView];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self loadData];
    
    
}
- (void)setNav
{
    self.title = @"我的银行卡";
    
    UIBarButtonItem *rightItemBtn = [UIBarButtonItem imageWithNormalImage:[UIImage imageNamed:@"wd-wodeyinhangka-tianjia"] hightImage:[UIImage imageNamed:@"wd-wodeyinhangka-tianjia"] addTarget:self action:@selector(goBindCard)];
    self.navigationItem.rightBarButtonItem = rightItemBtn;

}


- (void)goBindCard
{
    YDBindBankCardController *vc = [[YDBindBankCardController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)loadData
{
    
    [[SYProgressHUD sharedInstance] showMBHudWithSting:kLoadingText];
    [BaseNetRequest sessionRequestWithparameters:nil method:GETMethod requstType:BindBankCardList WithSuccessBlock:^(NSDictionary *resultDict) {

        [[SYProgressHUD sharedInstance] dismissMBWithTime:0];
        if ([resultDict[@"code"] integerValue] == 200) {
            self.tiedCardArray = [YDBankCardModel mj_objectArrayWithKeyValuesArray:resultDict[@"result"]];
            
            [self.tableView reloadData];
            
        }else{
            [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"信息加载出错，请重试~" isExistNav:YES];
            
        }
    } failBlock:^(NSError *error) {
       
        [[SYProgressHUD sharedInstance] dismissMBWithTime:0];
        [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"信息加载出错，请重试~" isExistNav:YES];
    }];
}

#pragma mark -UITableviewDelegate && UITableviewDatasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tiedCardArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YDBankListCell *cell = [YDBankListCell cellWithTableView:tableView];
    cell.cardModel = [self.tiedCardArray objectAtIndex:indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.tiedCardArray.count) {
        YDUnBindCardViewController *vc = [[YDUnBindCardViewController alloc] init];
        vc.isHideUnBindBtn = self.tiedCardArray.count == 1 ? YES : NO;
        vc.cardModel = [self.tiedCardArray objectAtIndex:indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    }
   
}
/*******tableView********/
-(UITableView *)tableView
{
    if (_tableView == nil) {
        
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, LKScreenW, self.view.height - self.tabBarController.tabBar.height) style:UITableViewStylePlain];
        [self.view addSubview:tableView];
        tableView.backgroundColor = NumberColor(255, 255, 255);
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.estimatedRowHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        tableView.estimatedSectionHeaderHeight = 0;
        tableView.rowHeight = isPhoneX ? rateX812(100) :rate750(100);
        tableView.tableFooterView = [UIView new];
        if (isIos11) {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView = tableView;
    }
    return _tableView;
}


@end
